<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\widgets\DatePicker;
use kartik\file\FileInput;
use yii\web\JsExpression;
use common\models\business\BusinessAdress;

use backend\assets\RotateAsset;
RotateAsset::register($this);
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessFarmers */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <div class="adv-table editable-table ">
                <div class="business-farmers-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ]
                    ]); ?>


                        <?= $form->field($model, 'gender')->dropDownList(['男'=>'男','女'=>'女'],['style'=>'width:50%;']) ?>

                        <?= $form->field($model, 'name')->textInput(['rows'=>3,'style'=>'width:50%']) ?>



                        <?= $form->field($model, 'identity')->textInput(['maxlength' => true,'style'=>'width:50%']) ?>

                        <?= $form->field($model, 'ischeckiendtity')->radioList(['校验身份证','确认填写正确，不校验身份证'],['maxlength' => true,'style'=>'width:50%','value'=>0]) ?>


                        <?=$form->field($model, 'countyid')->dropDownList(\yii\helpers\ArrayHelper::map(BusinessAdress::allCounty(),'id','name'),
                        [
                            'style'=>'width:150px',
                            'prompt'=>'请选择县',
                            'onchange'=>' $.post("/business/farmers/address-data?id='.'"+$(this).val(),function(data){
                                         var html ="";
                                         $.each(data,function (index, el){
                                            html += "<option value="+el.id+">"+el.name+"</option>";
                                         });
                                         $("#businessfarmers-townid").html(html);
                                    });',
                        ])->label('县'); ?>

                        <?=$form->field($model, 'townid')->dropDownList(\yii\helpers\ArrayHelper::map(BusinessAdress::allTown(),'id','name'),
                            [
                                'style'=>'width:150px',
                                'prompt'=>'请选择镇',
                                'onchange'=>' $.post("/business/farmers/address-data?id='.'"+$(this).val(),function(data){
                                         var html ="";
                                         $.each(data,function (index, el){
                                            html += "<option value="+el.id+">"+el.name+"</option>";
                                         });
                                         $("#businessfarmers-villageid").html(html);
                                    });',
                            ])->label('镇'); ?>

                        <?=$form->field($model, 'villageid')->dropDownList(\yii\helpers\ArrayHelper::map(BusinessAdress::allVillage(),'id','name'),
                        [
                                'style'=>'width:150px',
                                'prompt'=>'请选择村',
                                 'onchange'=>' $.get("/business/farmers/search-location?id='.'"+$(this).val(),function(data){
                                         $("#businessfarmers-lng").val(data.lng);
                                         $("#businessfarmers-lat").val(data.lat);
                                    });',
                        ])->label('村'); ?>


                        <?= $form->field($model, 'address')->textInput(['maxlength' => true,'style'=>'width:50%']) ?>




                        <?= $form->field($model, 'status')->dropDownList(\common\models\business\BusinessFarmers::$statues,['style'=>'width:50%']) ?>

                        <?= $form->field($model, 'ispoor')->dropDownList(\common\models\business\BusinessFarmers::$ispoor,['style'=>'width:50%']) ?>



                    <?= $form->field($model, 'lng')->textInput(['style'=>'width:50%']) ?>

                        <?= $form->field($model, 'lat')->textInput(['maxlength' => true,'style'=>'width:50%']) ?>

                        <?= $form->field($model, 'phone')->textInput(['maxlength' => true,'style'=>'width:50%']) ?>

                        <?= $form->field($model, 'introducer')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入该农户负责人姓名 ...'],
                            'initValueText'=>$model->person?$model->person->name:'',
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-person',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                            ],
                        ]);
                        ?>

                        <?= $form->field($model, 'pigs')->widget(\kartik\select2\Select2::className(), [
                            'initValueText'=>'',
                            'data' =>$ids,
                            'pluginOptions' => [
                                'tags' => true,
                                'allowClear' => true,
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-noliverypig',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true,
                                ],
                                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                'templateResult' => new JsExpression('function(res) { return res.text; }'),
                                'templateSelection' => new JsExpression('function (res) { console.log(res); return res.text; }'),
                                'maximumInputLength' => 10
                            ],
                            'options' => ['placeholder' => '输入对应的仔猪编号', 'multiple' => true],
                        ]);
                        ?>




                    <?= $form->field($model, 'pighousedesc')->textarea() ?>
                        <?= $form->field($model, 'descripation')->textarea() ?>





                    <?= $form->field($model, 'identity_photo[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->identity_photo,true,'identity_photo'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'identity_photo',\common\models\business\BusinessFarmers::tableName(),$model->id),
                                // 是否展示预览图
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                                'maxFileSize' => 10000
                            ],
                        ]); ?>

                        <?= $form->field($model, 'photo[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->photo,true,'photo'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'photo',\common\models\business\BusinessFarmers::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                                'maxFileSize' => 5000
                            ],
                        ]); ?>



                    <?= $form->field($model, 'pighousephoto[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->pighousephoto,true,'pighousephoto'),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'pighousephoto',\common\models\business\BusinessFarmers::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                                'maxFileSize' => 5000,
                                'previewZoomButtonIcons' => [
                                    'prev'=> '<i class="glyphicon glyphicon-triangle-left"></i>',
                                    'next'=> '<i class="glyphicon glyphicon-triangle-right"></i>',
                                    'toggleheader'=> '<i class="glyphicon glyphicon-resize-vertical"></i>',
                                    'fullscreen'=> '<i class="glyphicon glyphicon-fullscreen"></i>',
                                    'borderless'=> '<i class="glyphicon glyphicon-resize-full"></i>',
                                    'close'=> '<i class="glyphicon glyphicon-remove"></i>',
                                ]
                            ],
                        ]); ?>



                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>

<script>
    <?php $this->beginBlock('js_end') ?>


    /**
     *
     * 根据输入的编号取出巡检员
     * @param id
     */
    function getPerson(id){
        $.get('get-person-by-pig',{id:id},function(res){
            console.log(res);
        });
    }


    $('.field-businessfarmers-pighousephoto').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
        console.log('File sorted ', params.previewId, params.oldIndex, params.newIndex, params.stack);
    });

    $('.field-businessfarmers-identity_photo').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
        console.log('File sorted ', params.previewId, params.oldIndex, params.newIndex, params.stack);
    });

    $('.field-businessfarmers-photo').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
        console.log('File sorted ', params.previewId, params.oldIndex, params.newIndex, params.stack);
    });

    //旋转图片
    $('#businessfarmers-identity_photo').on('filezoomshow', function(event, params) {
        var str = '<button type="button" id="remoat" class="btn btn-default btn-remote" title="旋转" data-toggle="button" aria-pressed="false" autocomplete="off"><i class="glyphicon glyphicon-refresh"></i>旋转</button>'
        $('.btn-remote').remove();
        $('.btn-borderless').after(str);
        var num = 0;
        $('#remoat').click(function(){
            num ++;
            $('.file-zoom-content').rotate(90*num);
        });
        console.log('File zoom show ', params.sourceEvent, params.previewId, params.modal);
    });

    $('#businessfarmers-pighousephoto').on('filezoomshow', function(event, params) {
        var str = '<button type="button" id="remoat" class="btn btn-default btn-remote" title="旋转" data-toggle="button" aria-pressed="false" autocomplete="off"><i class="glyphicon glyphicon-refresh"></i>旋转</button>'
        $('.btn-remote').remove();

        $('.btn-borderless').after(str);

        var num = 0;
        $('#remoat').click(function(){
            num ++;
            $('.file-zoom-content').rotate(90*num);
        });
        console.log('File zoom show ', params.sourceEvent, params.previewId, params.modal);
    });

    $('#businessfarmers-photo').on('filezoomshow', function(event, params) {
        var str = '<button type="button" id="remoat" class="btn btn-default  btn-remote" title="旋转" data-toggle="button" aria-pressed="false" autocomplete="off"><i class="glyphicon glyphicon-refresh"></i>旋转</button>'
        $('.btn-remote').remove();
        $('.btn-borderless').after(str);
        var num = 0;
        $('#remoat').click(function(){
            num ++;
            $('.file-zoom-content').rotate(90*num);
        });
        console.log('File zoom show ', params.sourceEvent, params.previewId, params.modal);
    });


    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>